//
//  HRShowSoundCardRemendView.m
//  Hear
//
//  Created by mac on 2017/10/30.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowSoundCardRemendView.h"
#import "NSMutableAttributedString+GDAttString.h"
@interface HRShowSoundCardRemendView ()
@property (nonatomic, copy) EnterSoundExampleBLock enterSoundExampleBLock;
@end
@implementation HRShowSoundCardRemendView

+(void)ShowSoundCardRemendView:(EnterSoundExampleBLock)enterSoundExampleBLock;{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowSoundCardRemendView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowSoundCardRemendView class]]){
        
        
        HRShowSoundCardRemendView *shareView = (HRShowSoundCardRemendView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:enterSoundExampleBLock];
        
    }
}

- (void)loadView:(EnterSoundExampleBLock)enterSoundExampleBLock;{
    self.enterSoundExampleBLock = enterSoundExampleBLock;
    
//    UITapGestureRecognizer *closeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeView)];
//    [self addGestureRecognizer:closeTap];
    
    self.alpha = 0;
    
    CGFloat leftPading = 25;
    CGFloat contentHeight = 410;
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(leftPading, (kFullHeight-contentHeight)/2, kFullWidth-leftPading*2, contentHeight)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    contentView.layer.cornerRadius = 8;
    contentView.clipsToBounds = YES;
    contentView.alpha = 0;
    
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1;
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    
    UIImageView *topImageView = [UIImageView new];
    topImageView.image = [UIImage imageNamed:@"soundCardTop"];
    [contentView addSubview:topImageView];
    topImageView.frame = CGRectMake(10, leftPading, contentView.width-20, 60);
    
    
    UILabel *contentLabel = [UILabel new];
    contentLabel.attributedText = [NSMutableAttributedString arrtLabelText:GuidingText arrFont:CustomUIFont(14) attColor:FirstTextColor lineLengh:5 alignment:0 colorLenghFrom:0 colorLengh:0];
    [contentLabel sizeToFit];
    contentLabel.numberOfLines = 0;
    contentLabel.font = CustomUIFont(14);
    contentLabel.textColor = FirstTextColor;
    [contentView  addSubview:contentLabel];
    contentLabel.frame = CGRectMake(topImageView.left, topImageView.bottom+leftPading, topImageView.width, 160);
    
    
    
    UIButton *exampleButton = [UIButton buttonWithType:0];
    exampleButton.hidden = ![SampleText length];
    [exampleButton setTitle:SampleText forState:0];
    [exampleButton setTitleColor:BlueColor forState:0];
    exampleButton.titleLabel.font = CustomUIFont(15);
    [exampleButton addTarget:self action:@selector(exampleButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:exampleButton];
    exampleButton.layer.borderColor = BlueColor.CGColor;
    exampleButton.layer.borderWidth = 1;
    exampleButton.clipsToBounds = YES;
    exampleButton.layer.cornerRadius = 8;
    exampleButton.frame = CGRectMake(leftPading*3, contentLabel.bottom+leftPading, contentView.width-leftPading*6, 35);
    
    
    UIButton *recodButton = [UIButton buttonWithType:0];
    [recodButton setTitle:@"去录制" forState:0];
    [recodButton setTitleColor:[UIColor whiteColor] forState:0];
    recodButton.titleLabel.font = CustomUIFont(18);
    [recodButton addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:recodButton];
    recodButton.backgroundColor = BlueColor;
    recodButton.frame = CGRectMake(0, contentView.height-50, contentView.width, 50);
    
}

- (void)exampleButtonClick{
    [self closeView];
    self.enterSoundExampleBLock();
}

-(void)closeView
{
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


@end
